package fr.matwizz.mesdevoirs.android.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import fr.matwizz.mesdevoirs.android.adapter.AgendaAdapter;
import fr.matwizz.mesdevoirs.android.callback.TaskCallback;
import fr.matwizz.mesdevoirs.android.task.impl.RetrieveAgendaTask;
import fr.matwizz.mesdevoirs.app.R;
import fr.matwizz.mesdevoirs.serveur.model.Agenda;

public class MainActivity extends Activity {

    private Button selectAgendaBtn;
    private AgendaAdapter agendaAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.activity_main);
        agendaAdapter = new AgendaAdapter((ListView) findViewById(R.id.agendas), this);
        selectAgendaBtn = (Button) findViewById(R.id.btnAgenda);
        initializeListeners();
    }

    private void initializeListeners() {
        selectAgendaBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                recupereAgendas();
            }
        });
    }

    private void recupereAgendas() {
        new RetrieveAgendaTask(new TaskCallback<Agenda>() {
            @Override
            public void callback(Agenda agenda) {
                List<Agenda> agendas = new ArrayList<Agenda>();
                agendas.add(agenda);
                agendaAdapter.setAgendas(agendas);
            }
        }).execute("http://10.0.2.2:8080/mesdevoirs/rest/agenda/5629499534213120");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {

            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
